import type PageModal from '@/components/page-modal/page-modal.vue'
import { ref } from 'vue'

type CallbackFnType = (data?: any) => void

function usePageModal(newCallback?: CallbackFnType, editCallback?: CallbackFnType) {
  const modalRef = ref<InstanceType<typeof PageModal>>()

  function handleCreateBtnClick() {
    modalRef.value?.setModalVisible(false)
    newCallback && newCallback()
  }

  function handleEditBtnClick(itemData: any) {
    modalRef.value?.setModalVisible(true, itemData)
    // 调用回调函数
    if (editCallback) {
      editCallback(itemData)
    }
  }

  return {
    modalRef,
    handleCreateBtnClick,
    handleEditBtnClick
  }
}

export default usePageModal
